﻿using System;
using System.Data;
using EOSCRM.Web.Common;
using EOSCRM.Util;
using EOSCRM.Dao;
using Microsoft.Reporting.WebForms;

namespace EOSCRM.Web.Forms.DongHo.BaoCao
{
    public partial class KhachHangThayDHBaoHanh : Authentication
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    //TODO: Load references
                    LoadReferences();
                }
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }

        private void LoadReferences()
        {
            txtTuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtDenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
            CommonFunc.BindDropDownList(ddlChiNhanh, new ChiNhanhDao().GetList(), "TENCHINHANH", "MACHINHANH", true);
        }

        protected void btnBaoCao_Click(object sender, EventArgs e)
        {
            LayBaoCao();
        }
        private void LayBaoCao()
        {
            var TuNgay = ConvertUtil.ToDateTime(txtTuNgay.Text.Trim());
            var DenNgay = ConvertUtil.ToDateTime(txtDenNgay.Text.Trim());
            var myds = new ReportClass().DH_DanhSachThayDongHo(TuNgay.Value, DenNgay.Value, ddlChiNhanh.SelectedValue);
            if (myds == null) { CloseWaitingDialog(); return; }
            ReportDataSource ds = new ReportDataSource("DanhSachThayDongHo", myds.Tables[0]);
            rpViewer.LocalReport.DataSources.Clear();
            rpViewer.LocalReport.ReportPath = "Reports/DongHo/DanhSachThayDongHo.rdlc";
            rpViewer.LocalReport.DataSources.Add(ds);
            ReportParameter prNguoiLap = new ReportParameter("rpNguoiLap", LoginInfo.NHANVIEN.HOTEN.ToString());
            ReportParameter prNgayThangNam = new ReportParameter("rpNgayLap", ReportFuntions.GetNgayThangNam());
            var tieude = string.Format("THỐNG KÊ TÌNH HÌNH THAY ĐỒNG HỒ CHI NHÁNH {0} ", ddlChiNhanh.SelectedItem.Text.ToUpper());
            if (txtTuNgay.Text != txtDenNgay.Text)
            {
                tieude = tieude + string.Format("TỪ NGÀY {0} ĐẾN NGÀY {1}", txtTuNgay.Text.Trim(), txtDenNgay.Text.Trim());
            }
            else
            {
                tieude = tieude + string.Format("NGÀY {0}", txtTuNgay.Text.Trim());
            }
            ReportParameter prTieuDe = new ReportParameter("rpTitle", tieude);
            rpViewer.LocalReport.SetParameters(new[] { prNguoiLap, prNgayThangNam, prTieuDe });
            rpViewer.LocalReport.Refresh();
            divCR.Visible = true;
            upnlReport.Update();
            CloseWaitingDialog();
        }
    }
}